﻿using Abp.EntityFramework;
using GN.Pay.Core.Entities;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace GN.Pay.EntityFramework
{
    /// <summary>
    /// 数据库上下文
    /// </summary>   
    public class PayDbContext : GN.EntityFramework.GNDbContext
    {
        /// <summary>
        /// 系统配置数据集
        /// </summary>
        public virtual IDbSet<SystemConfigure> SystemConfigureDbSet { get; set; }

        /// <summary>
        /// 系统常量数据集
        /// </summary>
        public virtual IDbSet<SystemConstant> SystemConstantDbSet { get; set; }

        /// <summary>
        /// 系统常量组数据集
        /// </summary>
        public virtual IDbSet<SystemConstantGroup> SystemConstantGroupDbSet { get; set; }

        /// <summary>
        /// 交易账簿数据集
        /// </summary>
        public virtual IDbSet<TradeAccountBook> TradeAccountBookDbSet { get; set; }

        /// <summary>
        /// 交易账簿通道数据集
        /// </summary>
        public virtual IDbSet<TradeAccountBookChannel> TradeAccountBookChannelDbSet { get; set; }

        /// <summary>
        /// 交易通知数据集
        /// </summary>
        public virtual IDbSet<TradeNotify> TradeNotifyDbSet { get; set; }

        /// <summary>
        /// 交易支付申请数据集
        /// </summary>
        public virtual IDbSet<TradePayApply> TradePayApplyDbSet { get; set; }

        /// <summary>
        /// 交易支付申请通道数据集
        /// </summary>
        public virtual IDbSet<TradePayApplyChannel> TradePayApplyChannelDbSet { get; set; }

        /// <summary>
        /// 交易退款申请数据集
        /// </summary>
        public virtual IDbSet<TradeRefundApply> TradeRefundApplyDbSet { get; set; }

        /// <summary>
        /// 交易退款账簿数据集
        /// </summary>
        public virtual IDbSet<TradeRefundBook> TradeRefundBookDbSet { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //交易通道
            modelBuilder.Entity<TradeChannel>();

            //交易申请-应用关系
            modelBuilder.Entity<TradePayApply>();

            //交易付款申请通道-交易通道关系
            var table = modelBuilder.Entity<TradePayApplyChannel>();

            //交易支付申请通道-交易支付申请
            table.HasRequired(pA => pA.TradePayApply)
           .WithMany().HasForeignKey(pAC => pAC.ApplyId);


            //交易账簿通道-交易通道关系
            modelBuilder.Entity<TradeAccountBookChannel>();

            //交易账簿通道-交易账簿关系
            modelBuilder.Entity<TradeAccountBookChannel>()
            .HasRequired(r => r.TradeAccountBook)
            .WithMany()
            .HasForeignKey(rB => rB.BookId);

            //交易退款申请-应用关系
            modelBuilder.Entity<TradeRefundApply>();

            //交易退款账簿-交易通道关系
            modelBuilder.Entity<TradeRefundBook>();

            //交易转账申请-应用关系
            modelBuilder.Entity<TradeTransfersApply>();

            //交易转账账簿-交易通道关系
            modelBuilder.Entity<TradeTransfersBook>();

            //交易账簿-应用关系
            modelBuilder.Entity<TradeAccountBook>()
           .HasRequired(a => a.MerchantApp)
           .WithMany()
           .HasForeignKey(aB => aB.AppId);

            modelBuilder.Entity<MerchantInfo>();
            modelBuilder.Entity<MerchantAppInfo>();
            modelBuilder.Entity<MerchantAppChannel>();
            modelBuilder.Entity<MerchantAppSmsTemplate>();

            modelBuilder.Entity<MerchantMember>();
            modelBuilder.Entity<QueueErrorMessage>();

            modelBuilder.Entity<BankInfo>();

            modelBuilder.Entity<MerchantMemberBankCard>();



        }


        //TODO: Define an IDbSet for each Entity...

        //Example:
        //public virtual IDbSet<User> Users { get; set; }

        /* NOTE: 
         *   Setting "Default" to base class helps us when working migration commands on Package Manager Console.
         *   But it may cause problems when working Migrate.exe of EF. If you will apply migrations on command line, do not
         *   pass connection string name to base classes. ABP works either way.
         */
        public PayDbContext()
            : base("GNPay")
        {
            //this.Configuration.LazyLoadingEnabled = false;
        }

        /* NOTE:
         *   This constructor is used by ABP to pass connection string defined in PayDataModule.PreInitialize.
         *   Notice that, actually you will not directly create an instance of PayDbContext since ABP automatically handles it.
         */
        public PayDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
            // this.Configuration.LazyLoadingEnabled = false;
        }
    }
}
